package sas

import (
	"time"

	"github.com/micro-plat/lib4go/security/sha1"
	"github.com/micro-plat/lib4go/types"
)

//Sign 生成签名，chain为参数间连接符，kv为键值间连接符
func (n *sasClient) Sha1Sign(input types.XMap) (sign string, data types.XMap, err error) {
	input.SetValue(n.getTimespanName(), time.Now().Format("20060102150405"))
	input.SetValue(n.getEUIDName(), n.euid)
	return n.sha1Sign(input)
}

func (n *sasClient) sha1Sign(input types.XMap) (sign string, data types.XMap, err error) {
	//获取密钥
	res, err := n.SecretGet(SHA1Str)
	if err != nil {
		return
	}

	return n.sha1SignKey(input, res.Sha1Secret)
}

func (n *sasClient) sha1SignKey(input types.XMap, key string) (sign string, data types.XMap, err error) {
	raw := n.buildRaw(input, key, SHA1Str)
	sign = encrypt(raw)
	input.SetValue(n.getSignName(), sign)
	data = input
	return
}

//Encrypt SHA1哈希加密
func encrypt(raw string) string {
	return sha1.Encrypt(raw)
}
